<!-- 卡片表格 -->
<template>
  <div class="dd-card-table" ref="cardTableContent">
    <!-- 搜索栏 -->
    <div class="dd-card-table-search" :style="{'display' : !option.search ? 'none' : ''}">
      <el-form :inline="true" :model="screen">
        <el-row>
          <el-col :span="20" :xs="15" :sm="17" :lg="17" :xl="20">
            <div :class="expand == true ? 'condition-all' : 'condition-part'">
              <slot name="condition"></slot>
            </div>
          </el-col>
          <el-col :span="4" :xs="9" :sm="7" :lg="7" :xl="4">
            <div class="search-btn" >
              <slot name="search-operation"></slot>
              <el-form-item v-if="option.expandBtn">
                <el-button v-if="!expand" type="text" @click="expand = true">展开</el-button>
                <el-button v-if="expand" type="text" @click="expand = false">收起</el-button>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 表格内容 -->
    <div class="dd-card-table-content">
      <!-- 表格按钮 -->
      <div class="dd-card-table-operation" v-if="option.operation || option.title">
        <div class="dd-card-table-title" v-if="option.title">{{ option.title }}</div>
        <div class="table-btn" v-if="option.operation">
          <slot name="table-operation"></slot>
        </div>
      </div>
      <!-- 主体内容 -->
      <div class="dd-card-table-data" v-loading="option.loading" :style="'height: '+ contentHeight+ 'px; overflow-x: hidden; overflow-y: scroll;'">
        <slot></slot>
        <!-- 数据为空 -->
        <div class="dd-card-table-empty" v-if="!option.total">
          <div class="empty-box">
            <img class="empty-img" src="@/assets/images/empty.png" alt="">
            <div class="empty-tip">
              无数据
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 翻页器 -->
    <div class="dd-card-table-page" v-if="!option.pageNumber">
      <div class="page-current-total">
        共{{ option.total }}条记录  第{{ screen.pageIndex }}/{{ Math.ceil(option.total/screen.pageSize) }}页
      </div>
      <el-pagination background layout="prev, pager, next, jumper" :current-page="screen.pageIndex" :page-size="screen.pageSize" :total="option.total" @current-change="currentChange" @size-change="sizeChange">
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: 'dd-card-table',
  props: {
    screen: { // 表格筛选信息
      type: Object,
      default: () => {}
    },
    data: { // 列表数据
      type: Array,
      default: () => []
    },
    height: {
      type: String,
      default: '300px'
    },
    option: { // 表格配置信息
      type: Object,
      default: () => {
        return {
          total: 0, //表格总数据
          loading: true, //加载表格信息
          notDataText: '暂无数据',
          pageNumber: true,
          dialogTable: true,
          search: true,
          operation: true,
          expandBtn: true,
          title: ''
        }
      }
    },
    loadingData: Function, //加载数据方法
    showNumber: { //是否显示序列号
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      selectionData: [],
      contentHeight: 400,
      expand: false, // 是否展开搜索条件
      subHeight: 300, // 表格头部高度
    }
  },
  mounted() {
    this.$nextTick(() => {
      // 307 search 60 operation 50 pageNumber 50
      this.subHeight = !this.option.search && !this.option.operation && !this.option.title && this.option.pageNumber ? 147 : 
                       (!this.option.search && !this.option.operation && !this.option.title ? 197 :
                       (!this.option.search && this.option.pageNumber ? 197 :
                       (!this.option.operation && !this.option.title && this.option.pageNumber ? 207 :
                       (!this.option.search ? 247 :
                       (!this.option.operation && !this.option.title ? 257 :
                       (this.option.pageNumber ? 257 : 307))))));
      this.getHeight()
    })
  },
  methods: {
    getHeight(e) {
      if (this.option.dialogTable) {
        this.contentHeight = this.height
      } else {
        this.contentHeight = window.innerHeight - this.$refs.cardTableContent.offsetTop - this.subHeight;
        window.onresize = () => {
          this.contentHeight = window.innerHeight - this.$refs.cardTableContent.offsetTop - this.subHeight;
        }
      }
      e => e.preventDefault()
    },

    //加载信息
    loadingInfo: function () {
      this.option.loading = true;
      setTimeout(() => {
        this.loadingData();
      }, 200);
    },

    //查询信息
    checkInfo: function () {
      this.screen.pageIndex = 1;
      this.loadingInfo();
    },

    currentChange: function (current) {
      this.option.loading = true;
      this.screen.pageIndex = current;
      this.loadingInfo();
    },

    sizeChange: function (size) {
      this.option.loading = true;
      this.screen.pageIndex = 1;
      this.screen.pageSize = size;
      this.loadingInfo();
    }
  }
}
</script>
<style lang="scss" scoped>
.dd-card-table {
  display: flex;
  flex-direction: column;
  position: relative;
  /* height: 93%; */
}

.dd-card-table-search {
  width: calc(100% - 20px);
  padding: 10px;
  background-color: #fff;
  border-radius: 6px;
}
.condition-all {
  display: inline;
}
.condition-part {
  display: inline-block;
  overflow: hidden;
  max-height: 36px;
}

.dd-card-table-search .search-btn {
  float: right;
}

.dd-card-table-content {
  flex: 1;
  margin-top: 10px;
}

.dd-card-table-operation {
  display: flex;
  align-items: center;
  padding: 5px 10px;
  margin-bottom: 10px;
  line-height: 30px;

  .dd-card-table-title {
    display: inline-block;
  }

  .table-btn {
    flex: 1;
    text-align: right;
  }
}

.dd-card-table-empty {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;

  .empty-box {
    width: 100%;
    text-align: center;
  }
}

.dd-card-table-page {
  width: calc(100% - 20px);
  height: 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
  margin-top: 10px;
  font-size: 14px;
}
</style>